<template>
  <h2>当前求和：{{ sum }}</h2>
  <button @click="sum++">点击+1</button>
  <hr />
  <h2>当前信息为：{{ msg }}</h2>
  <button @click="msg += '?'">修改信息</button>
  <hr />
  <h2>姓名：{{ person.name }}</h2>
  <h2>年龄：{{ person.age }}</h2>
  <h2>职业：{{ person.job.j1.type }}</h2>
  <h2>薪资：{{ person.job.j1.salary }}K</h2>
  <button @click="person.name += '~'">修改姓名</button>
  <button @click="person.age++">修改年龄</button>
  <button @click="person.job.j1.salary++">加薪</button>
</template>
<script>
// Vue3特点为组合型API，所以要使用computed需要引入
import { ref, reactive, watch, watchEffect } from "vue";
export default {
  name: "Demo",
  setup(props, context) {
    // 数据
    let sum = ref(0);
    let msg = ref("你好啊！");
    let person = reactive({
      name: "张三",
      age: 18,
      job: {
        j1: {
          type: "前端",
          salary: 10,
        },
      },
    });

    //监视
    // watch(sum,(newVal,pervVal)=>{
    //   console.log('sum改变了',newVal,pervVal);
    // },{immediate:true})

    // 智能监视
    watchEffect(() => {
      console.log("watchEffect回调执行了");
      const x = sum.value       //这里用到了sum，就可以直接根据sum来触发type的改变
      person.job.j1.type += '!' //sum改变一次，type的值就改变一次
    });

    // 返回一个对象供模版使用
    return {
      sum,
      msg,
      person,
    };
  },
};
</script>

